package com.bluesmart.mia;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sys.DemoApp;
import com.util.Constant;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class NoteGoalActivity extends BaseActivity implements OnClickListener{
	String mFirstDo[] = {"哭","笑","爬","抬头","翻身","尿尿","咿哑语","叫妈妈","叫爸爸","坐","站","走","打针","受伤","吃药","长牙牙",
			"量体温","吃手指","会跳","会跑","摔跤","刷牙","游泳","坐车","滑滑梯","坐飞机","数数字","唱歌","画画","上课","公园","生日","理发"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_note_goal);

		((TextView) findViewById(R.id.center)).setText(getString(R.string.note));
		((Button) findViewById(R.id.right))
				.setCompoundDrawablesWithIntrinsicBounds(0, 0,
						R.drawable.a01, 0);
		
		((EditText)findViewById(R.id.content)).setText(mFirstDo[0]);
		
        WheelView number = (WheelView)findViewById(R.id.hourpicker);
        number.setViewAdapter(new ArrayWheelAdapter<String>(this, mFirstDo,Color.parseColor("#FFFFFF")));
        number.addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				((EditText)findViewById(R.id.content)).setText(mFirstDo[newValue]);
			}
        });
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.text0).setOnClickListener(this);
		((TextView)findViewById(R.id.text0)).setText(Utils.ConverToString(new Date(), "yyyy-MM-dd"));
	}

	@Override
	public void onClick(View arg0) {
		if(arg0.getId() == R.id.left) finish();
		else if(arg0.getId() == R.id.right) sendHttpRequest();
		else showDate(arg0.getId());
	}
	
	void showDate(final int index)
	{
		Calendar calendar = Calendar.getInstance();
		
		DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int day) {
            	String date = new StringBuilder().append(year).append("-")
                        .append((month + 1) < 10 ? "0" + (month + 1) : (month + 1))
                        .append("-")
                        .append((day < 10) ? "0" + day : day).toString();
            	
            	((TextView)findViewById(index)).setText(date);
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
        calendar.get(Calendar.DAY_OF_MONTH) );
		dialog.setTitle(getString(R.string.choose_date));
		dialog.show();
	}
	
	private void sendHttpRequest() {
		String text = ((EditText)findViewById(R.id.content)).getText().toString();
		if(text.length() == 0)
		{
			ToastUtil.showToastWithAlertPic(R.string.sign_in_tip);
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {

				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000"))
				{
					List<Activity> list = ((DemoApp) getApplicationContext()).getActivityList();
					((NoteViewActivity)list.get(list.size() - 2)).sendChartHttpRequest(true);
					
					finish();
					ToastUtil.showToastWithAlertPic(R.string.success);
				}
			}
		};
		
		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("babyid", ((DemoApp)getApplicationContext()).getSeletedBaby().getString("babyid"));
		params.put("notetype", "" + getIntent().getExtras().getInt("type"));
		params.put("handtype", "add");
		params.put("content", text);

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/NoteHandler.ashx", JSON.toJSONString(params), loginHandler);
	}
}
